package elka.sag.logisticsystem.actor;

import elka.sag.logisticsystem.message.ReservationMessage;
import org.apache.log4j.Logger;

import elka.sag.logisticsystem.message.ReservationResultMessage;
import elka.sag.logisticsystem.message.SimpleMessage;

public class WarehouseActor extends BaseActor {
	public static final String WAREHOUSE_ACTOR_CLASS = "elka.sag.logisticsystem.actor.WarehouseActor";

	private final static Logger LOGGER = Logger.getLogger(WarehouseActor.class);

	private String warehouseName;

	@Override
	protected void processMessage(SimpleMessage message) throws Exception {

        if(message instanceof ReservationMessage)   {
            LOGGER.info(getClass().getSimpleName() + " - " + warehouseName +" : Przyjeto zlecenie rezerawacji towaru dla zamowienia, orderId: "
                    + message.getOrderId());
            try {
                //tutaj sprawdzic czy jest dostepny towar (jesli dostepny to pomniejszyc jego liczbe o 1 i zwrocic 'true'.
                //jest to rownoznaczne z dolaczeniem towaru do zamowienia
                boolean wasReservationSuccessful = repositoryService.reserveArticle(warehouseName, ((ReservationMessage) message).getArticle().getId());

                //jezeli tak to w odpowiedzi ReservationResultMessage.isReserved = true, jezeli nie to false
                ReservationResultMessage result = new ReservationResultMessage(
                        message.getOrderId());

                result.setReserved(wasReservationSuccessful);

                getSender().tell(result, getSelf());

            } catch (Exception e) {
                getSender().tell(new akka.actor.Status.Failure(e), getSelf());
                throw e;
            }

        }
    }

	public String getWarehouseName() {
		return warehouseName;
	}

	public void setWarehouseName(String warehouseName) {
		this.warehouseName = warehouseName;
	}
}
